<?php
namespace User\Controller;
use Think\Controller;
/**
 * 子集详细列表页控制器 ListController.class.php
 */
class ListController extends CommonController{
    /* 属性声明 */
    //审批状态标记
    private $is_dealt = 0;
    
    //主配置文件
    private $main_conf_file = null;
    
    /* 方法声明 */
    /**
     * 构造方法
     */
    public function __construct() {
        parent::__construct();
        //检测用户合法性
        if(!$this -> uuid = session('uuid')){
            $this -> error('获取员工标识失败!');
        }
        $data = I('get.');

        //获取菜单项
        if(!$this -> menu = $data['menu']){
            $this -> error('非法参数传递!');
        }
        //获取企业号标识id
        if(!$corpid = $data['corpid']){
            $this -> error('未知corpid数据!');
        }

        $this -> corpid = $corpid;
        //全局共享corpid
        session('corpid',$corpid);
        $this -> main_conf_file = "./Org/Data/UserData/main_conf/{$this->corpid}_main.conf";
        //初始化前台js字典文件
        $this -> dic_file = './Org/Data/Dic_js/'.$this->corpid.'_dic.js';
        $this -> pro_file = './Org/Data/Dic_js/'.$this->corpid.'_pro.js';
    }
    
    /**
     * 入口方法
     */
    public function index(){

        //获取常规字典数据
        $this -> createDic();
        //分析是信息项是否在审核之中
        $this -> redis -> select(0);
        if($this->redis->hget('user_dealt',$this->uuid)){
           $this -> is_dealt = 1;
        }
        //成员数据
        $userinfo = $this ->getUserInfo($this -> uuid);

        //获取子集数据
        $dat = $this -> getSublist($userinfo,$this->menu);

        if(!$dat){
            $this -> error('未知menu数据!');
            exit;
        }
        //截取字符串caption长度
        $dat = $this -> cut_caption($dat);
        
        //是否可拓展标记 单值 多值
        $is_extension = $this -> menu == 'BO_CASC_HR_YGRS' ? 0 : 1;
        $this -> assign([
            'is_extension' => $is_extension,
            'caption' => $dat['caption'],
            'info' => json_encode($dat),
            'dic' => $this -> dic,
            'pro' => $this -> pro,
            'dealt' => $this -> is_dealt,
        ]);
        $this -> display('list');
    }
    
    /**
     * 生成字典数据
     */
    public function createDic(){
        $f1 = file_exists($this -> dic_file);
        $f2 = file_exists($this -> pro_file);
        if(!$f1 || !$f2){

            //获取通用字典
            $dic = $this -> dic();
            //获取省份字典数据
            $pro_dic = $this -> pro_dic();

            //生成前台js json文件 降低前台渲染数据压力
            $dic_file_data = 'var dic=' . $dic[1];
            $dic_file = './Org/Data/Dic_js/'.$this->corpid.'_dic.js';

            //检测通用字典js文件是否存在
            file_put_contents($dic_file, $dic_file_data);
            $pro_file_data = 'var pro_dic='.$pro_dic;
            $pro_file = './Org/Data/Dic_js/'.$this->corpid.'_pro.js';
            file_put_contents($pro_file, $pro_file_data);
        }

        $this -> dic = '/Org/Data/Dic_js/'.$this->corpid.'_dic.js';
        $this -> pro = '/Org/Data/Dic_js/'.$this->corpid.'_pro.js';
    }
    
    /**
     * 截取caption长度为8个汉字长度
     */
    private function cut_caption($dat){
        $list = $dat['sublist'];
        //过滤结构
        foreach($list as $k => $v){
           $list[$k]['caption'] = mb_substr($v['caption'],0,7,'utf-8');
        }
        $dat['sublist'] = $list;
        //过滤值结构
        $value_list = $dat['subvalue'];
        foreach($value_list as $k => $v){
            $info = $v['info'];
            foreach($info as $k2 => $v2){
                $info[$k2]['caption'] = mb_substr($v2['caption'],0,7,'utf-8');
            }
            $value_list[$k]['info'] = $info;
        }
        $dat['subvalue'] = $value_list;
        return $dat;
    }
    
    /**
     * 获取员工数据
     * @param string $uuid 员工唯一标识
     * @return array 员工全集数据
     */
    public function getUserInfo($uuid){
        $this -> redis -> select(0);

        if(!$data = $this -> redis -> hget('user_list',$uuid)){
            $this -> error('未匹配到您的相关信息!');
        }
        return json_decode($data,true)['information'];
    }
    
    /**
     * 获取指定子集数据
     * @param array $userinfo 成员数据
     * @param string $menu 子集名称
     * @return array 返回指定菜单过滤后子集数据
     */
    private function getSublist($userinfo,$menu){
        $item = null;
        foreach($userinfo as $k => $v){
            if($v['name'] == $menu){
                $item = $v;
                break;
            }
        }
        //检测是否存在主配置文件
        if(file_exists($this->main_conf_file)){
            $conf = unserialize(file_get_contents($this->main_conf_file));
            foreach($conf as $k => $v){
               if($v['name'] == $menu){
                   $conf = $v;
                   break;
               }
            }
            //0.过滤标准
            $filterlist = $conf['sublist'];
            //1.过滤sublist
            $sublist = $item['sublist'];
            $name_list = array_column($sublist,'name');
            $sublist_buf = [];
            //过滤子集显示项 + 排序操作
            foreach($filterlist as $k => $v){
                $offset = array_search($v,$name_list);
                $sublist_buf[] = $sublist[$offset];
            }
            
            //2.过滤subvalue
            $subvalue = $item['subvalue'];
            foreach($subvalue as $k => $v){
                $info = $v['info'];
                //定义临时数据缓冲区
                $subvalue_buf = [];
                $name_list = array_column($info,'name');
                foreach($filterlist as $k2 => $v2){
                    $offset = array_search($v2,$name_list);
                    $subvalue_buf[] = $info[$offset];
                }
                $subvalue[$k]['info'] = $subvalue_buf;
            }
            //3.数据重新组合
            //var_dump($subvalue);die;
            $item['sublist'] = $sublist_buf;
            $item['subvalue'] = $subvalue;
        }
        //返回最终数据
        return $item;
    }
    
    /**
     * 生成字典数据
     */
    public function dic(){
        $this -> user_dic_file = './Org/Data/UserData/dic_conf/'.$this->corpid.'_dic_conf.conf';
        //1.检测是否存在用户自定义字典配置
        if(file_exists($this->user_dic_file)){
            $data = json_decode(file_get_contents($this->user_dic_file),true);
            return [1,file_get_contents($this->user_dic_file)];
        }else{
        //2.返回默认字典数据
           $data = json_decode(file_get_contents('./Org/Data/init_dic.json'),true)['CASC.HR.DICT.ALL'];
           return [0,json_encode($data)];
        }
    }
    
    /**
     * 获取省份字典
     */
    public function pro_dic(){
        $this -> user_pro_file = './Org/Data/UserData/province/'.$this->corpid.'_pro_conf.conf';
        //检测是否存在省份字典配置文件
        if(file_exists($this -> user_pro_file)){
            return file_get_contents($this -> user_pro_file);
        }
        //返回默认省份字典文件
        return file_get_contents('./Org/Data/Original_Province.json');
    }
}